package com.peng.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.peng.repository.DepartmentRepository;
import com.peng.service.DepartmentService;
import com.peng.yygh.model.hosp.Department;
import com.peng.yygh.vo.hosp.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        String s = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(s, Department.class);
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (targetDepartment!=null){
            BeanUtils.copyProperties(department,targetDepartment,Department.class);
            targetDepartment.setUpdateTime(new Date());
            departmentRepository.save(targetDepartment);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(String hoscode, int page, int limit) {
        Pageable pageable= PageRequest.of(page-1,limit);
        ExampleMatcher matching = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example<Department> example=Example.of(department,matching);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (departmentByHoscodeAndDepcode!=null){
            departmentRepository.deleteById(departmentByHoscodeAndDepcode.getId());
        }

    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> departmentVos = new ArrayList<>();
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> of = Example.of(department);
        List<Department> all = departmentRepository.findAll(of);
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> stringListEntry : collect.entrySet()) {
            String key = stringListEntry.getKey();
            List<Department> value = stringListEntry.getValue();
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(value.get(0).getBigname());
            ArrayList<DepartmentVo> departmentVos1 = new ArrayList<>();
            for (Department department1 : value) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                departmentVos1.add(departmentVo1);
            }
            departmentVo.setChildren(departmentVos1);
            departmentVos.add(departmentVo);
        }
        return departmentVos;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department=departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null)
            return department.getDepname();
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}